﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformNet;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_FullPower : Form
	{
		private Form_MainSet parent = null;			// 父窗体引用
		private BaseCoreNet baseCoreNet = null;		// 底层平台内核引用
        private ZQConfig ZqConfig = null;			// ini配置辅助类

		private int devIndex = -1;					// 活跃设备索引
		private int subIndex = 0;					// 分控索引
		private int portIndex = 0;					// 端口索引
		private int miniIndex = -1;					// 灯具索引
        private string defaultFullPowerIni = "";	// 配置文件 defaultFullPower.ini 路径

		private string section = "FullPower";		// 多语言文件配置节名称

		private string tip = "提示";					// MessageBox 题头

		// 构造函数
		public Form_FullPower(BaseCoreNet baseCoreNet, Form_MainSet parent, int devIndex, int subIndex, int portIndex, int miniIndex)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            ZqConfig = new ZQConfig();
            
			// 配置文件路径
            defaultFullPowerIni = ZqConfig.GetAppPath() + @"\conf\defaultFullPower.ini";
			this.baseCoreNet = baseCoreNet;
			this.parent = parent;
			
			this.devIndex = devIndex;
			this.subIndex = subIndex;
			this.portIndex = portIndex;
			this.miniIndex = miniIndex;

			initEnvironment();
		}

		// 初始化运行环境
		private void initEnvironment()
		{
			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "fullPower.Text");
			fullPowerCheckBox.Text = LanguageOper.ReadValue(section, "fullPowerCheckBox.Text");
			greyGroupBox.Text = LanguageOper.ReadValue(section, "greyGroupBox.Text");
			greyRLabel.Text = LanguageOper.ReadValue(section, "greyRLabel.Text");
			greyGLabel.Text = LanguageOper.ReadValue(section, "greyGLabel.Text");
			greyBLabel.Text = LanguageOper.ReadValue(section, "greyBLabel.Text");
			greyWLabel.Text = LanguageOper.ReadValue(section, "greyWLabel.Text");
			targetLabel.Text = LanguageOper.ReadValue(section, "targetLabel.Text");
			pwmGroupBox.Text = LanguageOper.ReadValue(section, "pwmGroupBox.Text");
			detectLabel.Text = LanguageOper.ReadValue(section, "detectLabel.Text");
			pwmRLabel.Text = LanguageOper.ReadValue(section, "pwmRLabel.Text");
			pwmGLabel.Text = LanguageOper.ReadValue(section, "pwmGLabel.Text");
			pwmBLabel.Text = LanguageOper.ReadValue(section, "pwmBLabel.Text");
			pwmWLabel.Text = LanguageOper.ReadValue(section, "pwmWLabel.Text");
			configButton.Text = LanguageOper.ReadValue(section, "configButton.Text");
			exitButton.Text = LanguageOper.ReadValue(section, "exitButton.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				targetLabel.Location = new Point(208, targetLabel.Location.Y);
				detectLabel.Location = new Point(17, detectLabel.Location.Y);
			}
			else
			{
				targetLabel.Location = new Point(188, targetLabel.Location.Y);
				detectLabel.Location = new Point(5, detectLabel.Location.Y);
			}
		}

		// 窗体加载后处理
		private void Form_FullPower_Load(object sender, EventArgs e)
		{
			string powerTargetText = ZqConfig.ReadValue("Form_FullPower", "powerTargetTextBox", defaultFullPowerIni);
			string powerGrayText = ZqConfig.ReadValue("Form_FullPower", "powerGrayTextBox", defaultFullPowerIni);
			string powerRText = ZqConfig.ReadValue("Form_FullPower", "powerRTextBox", defaultFullPowerIni);
			string powerGText = ZqConfig.ReadValue("Form_FullPower", "powerGTextBox", defaultFullPowerIni);
			string powerBText = ZqConfig.ReadValue("Form_FullPower", "powerBTextBox", defaultFullPowerIni);
			string powerWText = ZqConfig.ReadValue("Form_FullPower", "powerWTextBox", defaultFullPowerIni);
			if (string.IsNullOrEmpty(powerTargetText) && string.IsNullOrEmpty(powerGrayText) && string.IsNullOrEmpty(powerRText)
				&& string.IsNullOrEmpty(powerGText) && string.IsNullOrEmpty(powerBText) && string.IsNullOrEmpty(powerWText))
			{
				powerTargetTextBox.Text = "24";
				powerGrayTextBox.Text = "255";
				powerRTextBox.Text = "14";
				powerGTextBox.Text = "18";
				powerBTextBox.Text = "18";
				powerWTextBox.Text = "18";
			}
			else
			{
				powerTargetTextBox.Text = powerTargetText;
				powerGrayTextBox.Text = powerGrayText;
				powerRTextBox.Text = powerRText;
				powerGTextBox.Text = powerGText;
				powerBTextBox.Text = powerBText;
				powerWTextBox.Text = powerWText;
			}
		}

		// 设置
		private void fullPowerSetbutton_Click(object sender, EventArgs e)
		{            
            // 保存目标功率和分色参数值
            if (string.IsNullOrEmpty(powerTargetTextBox.Text) || string.IsNullOrEmpty(powerGrayTextBox.Text) || string.IsNullOrEmpty(powerRTextBox.Text)
                || string.IsNullOrEmpty(powerGTextBox.Text) || string.IsNullOrEmpty(powerBTextBox.Text) || string.IsNullOrEmpty(powerWTextBox.Text))
            {
                string msg = "目标功率和分色参数值不能为空！";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }

                MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ZqConfig.WriteValue("Form_FullPower", powerTargetTextBox.Text, "powerTargetTextBox", defaultFullPowerIni);
                ZqConfig.WriteValue("Form_FullPower", powerGrayTextBox.Text, "powerGrayTextBox", defaultFullPowerIni);
                ZqConfig.WriteValue("Form_FullPower", powerRTextBox.Text, "powerRTextBox", defaultFullPowerIni);
                ZqConfig.WriteValue("Form_FullPower", powerGTextBox.Text, "powerGTextBox", defaultFullPowerIni);
                ZqConfig.WriteValue("Form_FullPower", powerBTextBox.Text, "powerBTextBox", defaultFullPowerIni);
                ZqConfig.WriteValue("Form_FullPower", powerWTextBox.Text, "powerWTextBox", defaultFullPowerIni);

                bool parseRes;
                int grayR, grayG, grayB, grayW, gray;
                double powerR, powerG, powerB, powerW, powerTarget;

                int pwmR, pwmG, pwmB, pwmW, pwmMax;
                double pwm, temp;

                int adjR, adjG, adjB, adjW;
                int K_Power_R, K_Power_G, K_Power_B, K_Power_W;

                parseRes = int.TryParse(grayRTextBox.Text.Trim(), out grayR);
                parseRes = int.TryParse(grayGTextBox.Text.Trim(), out grayG);
                parseRes = int.TryParse(grayBTextBox.Text.Trim(), out grayB);
                parseRes = int.TryParse(grayWTextBox.Text.Trim(), out grayW);
                parseRes = int.TryParse(powerGrayTextBox.Text.Trim(), out gray);
                parseRes = double.TryParse(powerRTextBox.Text.Trim(), out powerR);
                parseRes = double.TryParse(powerGTextBox.Text.Trim(), out powerG);
                parseRes = double.TryParse(powerBTextBox.Text.Trim(), out powerB);
                parseRes = double.TryParse(powerWTextBox.Text.Trim(), out powerW);
                parseRes = double.TryParse(powerTargetTextBox.Text.Trim(), out powerTarget);

                pwmR = grayR * grayR;
                pwmG = grayG * grayG;
                pwmB = grayB * grayB;
                pwmW = grayW * grayW;
                pwm = gray * gray;

                pwmMax = pwmR;

                if (pwmG > pwmMax)
                {
                    pwmMax = pwmG;
                }

                if (pwmB > pwmMax)
                {
                    pwmMax = pwmB;
                }

                if (pwmW > pwmMax)
                {
                    pwmMax = pwmW;
                }

                adjR = 100 * pwmR / pwmMax;
                adjG = 100 * pwmG / pwmMax;
                adjB = 100 * pwmB / pwmMax;
                adjW = 100 * pwmW / pwmMax;

                // 准备下发分色亮度
                byte[] currentPara = new byte[4];
                currentPara[0] = (byte)adjR;
                currentPara[1] = (byte)adjG;
                currentPara[2] = (byte)adjB;
                currentPara[3] = (byte)adjW;

                byte[] cmdData = new byte[256];

                cmdData[2] = 0x32;

                for (int i = 0; i < 4; i++)
                {
                    cmdData[8 + i] = currentPara[i];
                }

                // 如果全功率使能，下发分色亮度
                if (fullPowerCheckBox.Checked)
                {
                    baseCoreNet.sendMiniCmd(devIndex, subIndex, portIndex, miniIndex, cmdData, 16);
                }

                // 计算全功率调整系数
                temp = (powerR / powerTarget) * (65536 / pwm) * 8192;
                K_Power_R = (int)temp;
                temp = (powerG / powerTarget) * (65536 / pwm) * 8192;
                K_Power_G = (int)temp;
                temp = (powerB / powerTarget) * (65536 / pwm) * 8192;
                K_Power_B = (int)temp;
                temp = (powerW / powerTarget) * (65536 / pwm) * 8192;
                K_Power_W = (int)temp;

                // 计算全功率调整系数下发命令
                cmdData[2] = 0x4C;
                cmdData[8] = 0x27;
                cmdData[9] = 0x37;
                cmdData[10] = 0x09;
                cmdData[11] = 0x00;

                if (fullPowerCheckBox.Checked)
                {
                    cmdData[12] = 0x01;
                }
                else
                {
                    cmdData[12] = 0x00;
                }

                cmdData[13] = (byte)(K_Power_R >> 8);
                cmdData[14] = (byte)(K_Power_R);
                cmdData[15] = (byte)(K_Power_G >> 8);
                cmdData[16] = (byte)(K_Power_G);
                cmdData[17] = (byte)(K_Power_B >> 8);
                cmdData[18] = (byte)(K_Power_B);
                cmdData[19] = (byte)(K_Power_W >> 8);
                cmdData[20] = (byte)(K_Power_W);

                cmdData[20] = 0;
                cmdData[21] = 0;
                cmdData[22] = 0;
                cmdData[23] = 0;

                // 全功率调整系数下发
                baseCoreNet.sendMiniCmd(devIndex, subIndex, portIndex, miniIndex, cmdData, 24);

				string msg = "设置参数已下发";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.1");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
		}

		// 退出
		private void fullPowerExitbutton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
